home *** CD-ROM | disk | FTP | other *** search
/ C & C++ Multimedia Cyber Classroom / C and C++ Multimedia Cyber Classroom (Prentice Hall) (1998).iso / cpphtp2 / cpphtp2.jar / chpt_18.gml < prev    next >
Text File  |  1998-03-03  |  81KB  |  1,937 lines

  1. <html>
  2. <chapter>
  3. <section type=Popup name=Terminology title="Terminology">
  4. <page>
  5. <font size=14>
  6. A<br>
  7. append output symbol 
  8. (<b>>></b>) 
  9. <a href="%s2p3"><img src=iicons/bullbib.gif></a>
  10. <br>
  11. <b>argv</b> 
  12. <a href="%s4p0"><img src=iicons/bullbib.gif></a>
  13. <br>
  14. <b>atexit</b> 
  15. <a href="%s6p0"><img src=iicons/bullbib.gif></a>
  16. <a href="%s6p1"><img src=iicons/bullbib.gif></a>
  17. <br>
  18. C<br>
  19. <b>calloc</b> 
  20. <a href="%s10p0"><img src=iicons/bullbib.gif></a>
  21. <br>
  22. command-line 
  23. arguments 
  24. <a href="%s4p0"><img src=iicons/bullbib.gif></a>
  25. <a href="%s4p0"><img src=iicons/bullbib.gif></a>
  26. <br>
  27. D<br>
  28. dynamic arrays 
  29. <a href="%s10p1"><img src=iicons/bullbib.gif></a>
  30. <br>
  31. E<br>
  32. event 
  33. <a href="%s9p0"><img src=iicons/bullbib.gif></a>
  34. <br>
  35. <b>exit</b> 
  36. <a href="%s6p0"><img src=iicons/bullbib.gif></a>
  37. <a href="%s6p1"><img src=iicons/bullbib.gif></a>
  38. <br>
  39. </font>
  40.  
  41. </page>
  42. <page>
  43. <font size=14>
  44. <b>EXIT_FAILURE</b> 
  45. <a href="%s6p2"><img src=iicons/bullbib.gif></a>
  46. <br>
  47. <b>EXIT_SUCCESS</b> 
  48. <a href="%s6p1"><img src=iicons/bullbib.gif></a>
  49.  
  50. <br>
  51. <b>extern</b> 
  52. <a href="%s5p1"><img src=iicons/bullbib.gif></a>
  53. <br>
  54. <b>extern "C"</b> 
  55. <a href="%s13p1"><img src=iicons/bullbib.gif></a>
  56. <br>
  57. external linkage 
  58. <a href="%s5p5"><img src=iicons/bullbib.gif></a>
  59. <br>
  60. F<br>
  61. <b>F</b> floating-point suffix 
  62. <a href="%s8p1"><img src=iicons/bullbib.gif></a>
  63. <br>
  64. <b>f</b> floating-point suffix 
  65. <a href="%s8p1"><img src=iicons/bullbib.gif></a>
  66. <br>
  67. floating-point exception 
  68.  
  69. <a href="%s9p0"><img src=iicons/bullbib.gif></a>
  70. <br>
  71. G<br>
  72. <b>goto</b> statement 
  73. <a href="%s11p1"><img src=iicons/bullbib.gif></a>
  74. <br>
  75. I<br>
  76. </font>
  77.  
  78. </page>
  79. <page>
  80. <font size=14>
  81. illegal instruction 
  82. <a href="%s9p0"><img src=iicons/bullbib.gif></a>
  83. <a href="^Illustration::c:s0p2"><img src=iicons/bullbib.gif></a>
  84. <br>
  85. internal linkage 
  86. <a href="%s5p5"><img src=iicons/bullbib.gif></a>
  87. <br>
  88. interrupt 
  89. <a href="%s9p0"><img src=iicons/bullbib.gif></a>
  90. <br>
  91. L<br>
  92. <b>L</b> floating-point suffix 
  93.  
  94. <a href="%s8p1"><img src=iicons/bullbib.gif></a>
  95. <br>
  96. <b>l</b> floating-point suffix 
  97. <a href="%s8p1"><img src=iicons/bullbib.gif></a>
  98. <br>
  99. <b>L</b> integer suffix 
  100. <a href="%s8p0"><img src=iicons/bullbib.gif></a>
  101. <a href="%s8p0"><img src=iicons/bullbib.gif></a>
  102. <br>
  103. <b>l</b> integer suffix 
  104. <a href="%s8p0"><img src=iicons/bullbib.gif></a>
  105. <a href="%s8p0"><img src=iicons/bullbib.gif></a>
  106. <br>
  107. M<br>
  108. <b>make</b> 
  109. <a href="%s5p7"><img src=iicons/bullbib.gif></a>
  110. <br>
  111. <b>makefile</b> 
  112. <a href="%s5p7"><img src=iicons/bullbib.gif></a>
  113. <br>
  114. P<br>
  115. pipe (<b>|</b>) 
  116. <a href="%s2p2"><img src=iicons/bullbib.gif></a>
  117. <br>
  118. </font>
  119.  
  120. </page>
  121. <page>
  122. <font size=14>
  123. piping 
  124. <a href="%s2p2"><img src=iicons/bullbib.gif></a>
  125. <br>
  126. R<br>
  127. <b>raise</b> 
  128. <a href="%s9p1"><img src=iicons/bullbib.gif></a>
  129. <br>
  130. <b>realloc</b> 
  131. <a href="%s10p1"><img src=iicons/bullbib.gif></a>
  132. <br>
  133. redirect output symbol 
  134. (<b>></b>) 
  135. <a href="%s2p3"><img src=iicons/bullbib.gif></a>
  136. <br>
  137. S<br>
  138. segmentation violation 
  139.  
  140. <a href="%s9p0"><img src=iicons/bullbib.gif></a>
  141. <br>
  142. <b>signal</b> 
  143. <br>
  144. signal handling library 
  145.  
  146. <a href="%s9p0"><img src=iicons/bullbib.gif></a>
  147. <br>
  148. <b>signal.h</b> 
  149. <a href="%s9p1"><img src=iicons/bullbib.gif></a>
  150. <a href="^Illustration::c:s0p2"><img src=iicons/bullbib.gif></a>
  151. <br>
  152. <b>static</b> 
  153. <a href="%s5p4"><img src=iicons/bullbib.gif></a>
  154. <a href="%s12p3"><img src=iicons/bullbib.gif></a>
  155. <a href="%s12p5"><img src=iicons/bullbib.gif></a>
  156. <br>
  157. </font>
  158.  
  159. </page>
  160. <page>
  161. <font size=14>
  162. <b>stdarg.h</b> 
  163. <a href="%s3p1"><img src=iicons/bullbib.gif></a>
  164. <br>
  165. T<br>
  166. trap unexpected event 
  167. <a href="%s9p0"><img src=iicons/bullbib.gif></a>
  168. <br>
  169. U<br>
  170. <b>union</b> 
  171. <a href="%s12p0"><img src=iicons/bullbib.gif></a>
  172. <a href="%s12p1"><img src=iicons/bullbib.gif></a>
  173. <a href="^Errors::c:s0p2"><img src=iicons/bullbib.gif></a>
  174. <br>
  175. <b>unsigned</b> 
  176. <a href="%s8p0"><img src=iicons/bullbib.gif></a>
  177. <br>
  178. <b>unsigned long</b> 
  179. <a href="%s8p0"><img src=iicons/bullbib.gif></a>
  180. <a href="%s8p1"><img src=iicons/bullbib.gif></a>
  181. <br>
  182. V<br>
  183. <b>va_arg</b> 
  184. <a href="^Illustration::c:s0p1"><img src=iicons/bullbib.gif></a>
  185. <a href="^Illustration::c:s0p1"><img src=iicons/bullbib.gif></a>
  186. <a href="^Illustration::c:s0p1"><img src=iicons/bullbib.gif></a>
  187. <a href="%s3p1"><img src=iicons/bullbib.gif></a>
  188. <br>
  189. <b>va_end</b> 
  190. <a href="^Illustration::c:s0p1"><img src=iicons/bullbib.gif></a>
  191. <a href="^Illustration::c:s0p1"><img src=iicons/bullbib.gif></a>
  192. <a href="^Illustration::c:s0p1"><img src=iicons/bullbib.gif></a>
  193. <a href="%s3p1"><img src=iicons/bullbib.gif></a>
  194. <br>
  195. <b>va_list</b> 
  196. <a href="^Illustration::c:s0p1"><img src=iicons/bullbib.gif></a>
  197. <a href="^Illustration::c:s0p1"><img src=iicons/bullbib.gif></a>
  198. <a href="%s3p1"><img src=iicons/bullbib.gif></a>
  199. <br>
  200. <b>va_start</b> 
  201. <a href="^Illustration::c:s0p1"><img src=iicons/bullbib.gif></a>
  202. <a href="^Illustration::c:s0p1"><img src=iicons/bullbib.gif></a>
  203. <a href="%s3p1"><img src=iicons/bullbib.gif></a>
  204. <br>
  205. variable-length argument 
  206. list 
  207. <a href="^Illustration::c:s0p1"><img src=iicons/bullbib.gif></a>
  208. <br>
  209. </font>
  210.  
  211. </page>
  212. <page>
  213. <font size=14>
  214. <b>volatile</b> type qualifier 
  215. <a href="%s7p0"><img src=iicons/bullbib.gif></a>
  216. <br>
  217. <br>
  218. </font>
  219.  
  220. </page>
  221. </section>
  222. <section type=Popup name=Portable title="Portability">
  223. <page>
  224. Some systems do not 
  225. support global variable 
  226. names or function 
  227. names of more than 6 
  228. characters. This should 
  229. be considered when 
  230. writing programs that 
  231. will be ported to 
  232. multiple platforms.<br>
  233. <br>
  234.  
  235. </page>
  236. <page>
  237. If data is stored in a 
  238. <b>union</b> as one type and 
  239. referenced as another 
  240. type, the results are 
  241. implementation 
  242. dependent. <br>
  243. <br>
  244.  
  245. </page>
  246. <page>
  247. Some <b>unions</b> may not 
  248. port easily to other 
  249. computer systems. 
  250. Whether a <b>union</b> is 
  251. portable or not often 
  252. depends on the storage 
  253. alignment requirements 
  254. for the <b>union</b> member 
  255. data types on a given 
  256. system.<br>
  257.  
  258. </page>
  259. <page>
  260. The amount of storage 
  261. required to store a 
  262. <b>union</b> is 
  263. implementation 
  264. dependent. <br>
  265. <br>
  266.  
  267. </page>
  268. </section>
  269. <section type=Popup name=Quotes title="Quotes">
  270. <page>
  271. <i>We'll use a signal I have 
  272. tried and found</i> <br>
  273. <i>far-reaching and easy to 
  274. yell. Waa-hoo!</i> <br>
  275. Zane Grey<br>
  276. <br>
  277.  
  278. </page>
  279. <page>
  280. <i>Use it up, wear it out; 
  281. Make it do, or do without.</i> <br>
  282. Anonymous<br>
  283. <br>
  284.  
  285. </page>
  286. <page>
  287. <i>It is quite a three-pipe 
  288. problem.</i> <br>
  289. Sir Arthur Conan Doyle<br>
  290. <br>
  291.  
  292. </page>
  293. <page>
  294. <i>But yet an union in 
  295. partition;</i> <br>
  296. William Shakespeare<br>
  297. <br>
  298.  
  299. </page>
  300. <page>
  301. <i>I could never make out 
  302. what those damned dots 
  303. meant.</i> <br>
  304. Winston Churchill<br>
  305. <br>
  306.  
  307. </page>
  308. </section>
  309. <section type=Popup name=Illustration title="Illustrations">
  310. <page>
  311. <a href="^Illustration::c:s0p1">Fig. 18.1</a>  The type and the macros defined in header <b>stdarg.h</b>.<br>
  312. <a href="^Code::c:s0p0">Fig. 18.2</a>  Using variable-length argument lists.<br>
  313. <a href="^Code::c:s0p1">Fig. 18.3</a>  Using command-line arguments.<br>
  314. <a href="^Code::c:s0p2">Fig. 18.4</a>  Using functions <b>exit</b> and <b>atexit</b>.<br>
  315. <a href="^Illustration::c:s0p2">Fig. 18.5</a>  The signals defined in header <b>signal.h</b>.<br>
  316. <a href="^Code::c:s0p3">Fig. 18.6</a>  Using signal handling.<br>
  317. <a href="^Code::c:s0p4">Fig. 18.7</a>  Using <b>goto</b>.<br>
  318. <a href="^Code::c:s0p5">Fig. 18.8</a>  Printing the value of a <b>union</b> in both member data types.<br>
  319. <a href="^Code::c:s0p6">Fig. 18.9</a>  Using an anonymous <b>union</b>.<br>
  320. <br>
  321.  
  322. </page>
  323. <page>
  324. <font size=18>Fig<a href="~audio/Ch18/18fig001.au"><img src="bckgrnds/icons/audio.gif" align=sidebar></a>ure 18.1 - The type and the macros defined in header <b>stdarg.h</b>.<img src="graphics/ch18/fig18001.gif" ></font><br>
  325.  
  326. </page>
  327. <page>
  328. <font size=18><a href="~audio/Ch18/18fig005.au"><img src="bckgrnds/icons/audio.gif" align=sidebar></a>Figure 18.5 - The signals defined in header <b>signal.h</b>.<img src="graphics/ch18/fig18005.gif" ></font><br>
  329.  
  330. </page>
  331. </section>
  332. <section type=Popup name=Answers title="Answers">
  333. <page pagename="Answer 18.1">
  334. <b>Answer 18.1</b><br>
  335. a) redirect input (<b><</b>).<br>
  336. b) redirect output (<b>></b>).<br>
  337. c) append output (<b>>></b>).<br>
  338. d) pipe (<b>|</b>).<br>
  339. e) ellipsis (<b>...</b>).<br>
  340. f) <b>va_start</b>.<br>
  341. g) <b>va_arg</b>.<br>
  342. h) <b>va_end</b>.<br>
  343. i) <b>argc</b>.<br>
  344. j) <b>argv</b>.<br>
  345. <foreign  name="exercises" url="^Exercises::c:s0p0">
  346.  
  347. </page>
  348. <page pagename="Answer 18.1">
  349. k) <b>make</b>, <b>makefile</b>.<br>
  350. l) <b>exit</b>.<br>
  351. m) <b>atexit</b>.<br>
  352. n) <b>const</b>.<br>
  353. o) suffix.<br>
  354. p) <b>signal</b>.<br>
  355. q) <b>raise</b>.<br>
  356. r) <b>calloc</b>.<br>
  357. s) <b>realloc</b>.<br>
  358. t) union.<br>
  359. u) <b>union</b>. <br>
  360. <foreign  name="exercises" url="^Exercises::c:s0p0">
  361.  
  362. </page>
  363. <page pagename="Answer 18.3">
  364. <b>Answer 18.3</b><br>
  365. The solution to this exercise can be found on your Cyber Classroom CD. Copy 
  366. the file cpphtp2/answers/P18_03.zip to your hard drive and unzip the program 
  367. code.<br>
  368. <foreign  name="exercises" url="^Exercises::c:s0p5">
  369. <br>
  370.  
  371. </page>
  372. <page pagename="Answer 18.8">
  373. <b>Answer 18.8</b><br>
  374. The solution to this exercise can be found on your Cyber Classroom CD. Copy 
  375. the file cpphtp2/answers/P18_08.zip to your hard drive and unzip the program 
  376. code.<br>
  377. <foreign  name="exercises" url="^Exercises::c:s0p10">
  378. <br>
  379.  
  380. </page>
  381. <page pagename="Answer 18.10">
  382. <b>Answer 18.10</b><br>
  383. The solution to this exercise can be found on your Cyber Classroom CD. Copy 
  384. the file cpphtp2/answers/P18_10.zip to your hard drive and unzip the program 
  385. code.<br>
  386. <foreign  name="exercises" url="^Exercises::c:s0p13">
  387. <br>
  388.  
  389. </page>
  390. </section>
  391. <section type=Popup name=Exercises title="Exercises">
  392. <page pagename="Exercise 18.1">
  393. <b>Exercise 18.1</b><br>
  394. Fill in the blanks in each of the following:<br>
  395. a)  Symbol ________ redirects input data from the keyboard to come from a 
  396. file.<br>
  397. b)  The ________ symbol is used to redirect the screen output to be placed in a 
  398. file.<br>
  399. c)  The ________ symbol is used to append the output of a program to the end 
  400. of a file.<br>
  401. d)  A ________ is used to direct the output of a program as the input of another 
  402. program.<br>
  403. e)  An ________ in the parameter list of a function indicates that the function 
  404. can receive a variable number of arguments.<br>
  405. <foreign  name="answers" url="^Answers::c:s0p0">
  406.  
  407. </page>
  408. <page pagename="Exercise 18.1">
  409. f)  Macro ________ must be invoked before the arguments in a variable-length 
  410. argument list can be accessed.<br>
  411. g)  Macro ________ is used to access the individual arguments of a variable-
  412. length argument list.<br>
  413. h)  Macro ________ facilitates a normal return from a function whose variable 
  414. argument list was referred to by macro <b>va_start</b>.<br>
  415. i)  Argument ________ of <b>main</b> receives the number of arguments in a 
  416. command line.<br>
  417. j)  Argument ________ of <b>main</b> stores command-line arguments as character 
  418. strings.<br>
  419. <foreign  name="answers" url="^Answers::c:s0p0">
  420.  
  421. </page>
  422. <page pagename="Exercise 18.1">
  423. k)  The UNIX utility ________ reads a file called ________ that contains 
  424. instructions for compiling and linking a program consisting of multiple source 
  425. files. The utility only recompiles a file if the file has been modified since it was 
  426. last compiled.<br>
  427. l)  Function ________ forces a program to terminate execution.<br>
  428. m)  Function ________ registers a function to be called upon normal 
  429. termination of the program.<br>
  430. n)  Type qualifier ________ indicates that an object should not be modified 
  431. after it is initialized.<br>
  432. o)  An integer or floating-point ________ can be appended to an integer or 
  433. floating-point constant to specify the exact type of the constant.<br>
  434. p)  Function ________ can be used to trap unexpected events.<br>
  435. <foreign  name="answers" url="^Answers::c:s0p0">
  436.  
  437. </page>
  438. <page pagename="Exercise 18.1">
  439. q)  Function ________ generates a signal from within a program.<br>
  440. r)  Function ________ dynamically allocates memory for an array and 
  441. initializes the elements to zero.<br>
  442. s)  Function ________ changes the size of a block of dynamically allocated 
  443. memory.<br>
  444. t)  A ________ is a class containing a collection of variables that occupy the 
  445. same memory, but at different times.<br>
  446. u)  The ________ keyword is used to introduce a union definition.<br>
  447. <foreign  name="answers" url="^Answers::c:s0p0">
  448. <br>
  449.  
  450. </page>
  451. <page pagename="Exercise 18.2">
  452. <b>Exercise 18.2</b><br>
  453. Write a program that calculates the product of a series of integers that are passed 
  454. to function <b>product</b> using a variable-length argument list. Test your function 
  455. with several calls each with a different number of arguments.<br>
  456. <br>
  457. <br>
  458.  
  459. </page>
  460. <page pagename="Exercise 18.3">
  461. <b>Exercise 18.3</b><br>
  462. Write a program that prints the command-line arguments of the program. <br>
  463. <br>
  464. <foreign  name="answers" url="^Answers::c:s0p2">
  465. <br>
  466.  
  467. </page>
  468. <page pagename="Exercise 18.4">
  469. <b>Exercise 18.4</b><br>
  470. Write a program that sorts an integer array into ascending order or descending 
  471. order. The program should use command-line arguments to pass either argument 
  472. <b>-a</b> for ascending order or <b>-d </b>for descending order. (Note: This is the standard 
  473. format for passing options to a program in UNIX.)<br>
  474. <br>
  475. <br>
  476.  
  477. </page>
  478. <page pagename="Exercise 18.5">
  479. <b>Exercise 18.5</b><br>
  480. Read the manuals for your system to determine what signals are supported by 
  481. the signal handling library (<b>signal.h</b>). Write a program with signal handlers for 
  482. the signals <b>SIGABRT</b> and <b>SIGINT</b>. The program should test the trapping of 
  483. these signals by calling function <b>abort</b> to generate a signal of type <b>SIGABRT</b>, 
  484. and by typing <b><ctrl> c</b> to generate a signal of type <b>SIGINT</b>.<br>
  485. <br>
  486. <br>
  487.  
  488. </page>
  489. <page pagename="Exercise 18.6">
  490. <b>Exercise 18.6</b><br>
  491. Write a program that dynamically allocates an array of integers. The size of the 
  492. array should be input from the keyboard. The elements of the array should be 
  493. assigned values input from the keyboard. Print the values of the array. Next, 
  494. reallocate the memory for the array to half of the current number of elements. 
  495. Print the values remaining in the array to confirm that they match the first half of 
  496. the values in the original array. <br>
  497. <br>
  498.  
  499. </page>
  500. <page pagename="Exercise 18.7">
  501. <b>Exercise 18.7</b><br>
  502. Write a program that takes two file name command-line arguments, reads the 
  503. characters from the first file one at a time, and writes the characters in reverse 
  504. order to the second file.<br>
  505. <br>
  506. <br>
  507.  
  508. </page>
  509. <page pagename="Exercise 18.8">
  510. <b>Exercise 18.8</b><br>
  511. Write a program that uses goto statements to simulate a nested looping 
  512. structure that prints a square of asterisks, as follows:<br>
  513. <hr>
  514. <font size=2><br></font><font size=11><pre>
  515. *****<p>
  516. *   *<p>
  517. *   *<p>
  518. *   *<p>
  519. *****<p>
  520. </pre></font>
  521. <hr>
  522. The program should use only the following three output statements:<br>
  523. <font size=2><br></font><font size=11><pre>
  524. cout << '*';<p>
  525. </pre></font>
  526. <foreign  name="answers" url="^Answers::c:s0p3">
  527.  
  528. </page>
  529. <page pagename="Exercise 18.8">
  530. <font size=2><br></font><font size=11><pre>
  531. cout << ' ';<p>
  532. </pre></font>
  533. <font size=2><br></font><font size=11><pre>
  534. cout << endl; <p>
  535. </pre></font>
  536. <foreign  name="answers" url="^Answers::c:s0p3">
  537.  
  538. </page>
  539. <page pagename="Exercise 18.9">
  540. <b>Exercise 18.9</b><br>
  541. Provide the definition for <b>union Data</b> containing <b>char c</b>, <b>short s</b>, <b>long l</b>, <b>float f</b>, 
  542. and <b>double d</b>.<br>
  543. <br>
  544. <br>
  545.  
  546. </page>
  547. <page pagename="Exercise 18.10">
  548. <b>Exercise 18.10</b><br>
  549. Create <b>union Integer</b> with members <b>char c</b>, <b>short s</b>, <b>int i</b>, and <b>long l</b>. Write a 
  550. program that inputs values of type <b>char</b>, <b>short</b>, <b>int</b> and <b>long</b>, and stores the 
  551. values in <b>union</b> variables of type <b>union Integer</b>. Each <b>union</b> variable should be 
  552. printed as a <b>char</b>, a <b>short</b>, an <b>int</b>, and a <b>long</b>. Do the values always print 
  553. correctly?<br>
  554. <br>
  555. <foreign  name="answers" url="^Answers::c:s0p4">
  556. <br>
  557.  
  558. </page>
  559. <page pagename="Exercise 18.11">
  560. <b>Exercise 18.11</b><br>
  561. Create <b>union</b> <b>FloatingPoint</b> with members <b>float</b> <b>f</b>, <b>double</b> <b>d</b>, and <b>long double l</b>. 
  562. Write a program that inputs value of type <b>float</b>, <b>double</b>, and <b>long double</b>, and 
  563. stores the values in <b>union</b> variables of type <b>union FloatingPoint</b>. Each <b>union</b> 
  564. variable should be printed as a <b>float</b>, a <b>double</b>, and a <b>long double</b>. Do the values 
  565. always print correctly?<br>
  566. <br>
  567. <br>
  568.  
  569. </page>
  570. <page pagename="Exercise 18.12">
  571. <b>Exercise 18.12</b><br>
  572. Given the <tt><b>union
  573. </b></tt><br>
  574. <font size=2><br></font><font size=11><pre>
  575. union A {<p>
  576.    float y;<p>
  577.    char *z;<p>
  578. };<p>
  579. </pre></font>
  580. which of the following are correct statements for initializing the <tt><b>union</b></tt>?<br>
  581. <font size=2><br></font><font size=11><pre>
  582. a)  A p = B;  // B is of same type as A<p>
  583. b)  A q = x; // x is a float<p>
  584. c)  A r = 3.14159;<p>
  585. d)  A s = { 79.63 };<p>
  586. e)  A t = { "Hi There!" };<p>
  587. </pre></font>
  588.  
  589. </page>
  590. <page pagename="Exercise 18.12">
  591. <font size=2><br></font><font size=11><pre>
  592. f)  A u = { 3.14159, "Pi" };<p>
  593. </pre></font>
  594. <br>
  595. <br>
  596.  
  597. </page>
  598. </section>
  599. <section type=Popup name=Perform title="Performance">
  600. <page>
  601. Global variables 
  602. increase performance 
  603. because they can be 
  604. accessed directly by 
  605. any function--the 
  606. overhead of passing 
  607. data to functions is 
  608. eliminated.<br>
  609. <br>
  610.  
  611. </page>
  612. <page>
  613. The <b>goto</b> statement can 
  614. be used to exit deeply 
  615. nested control 
  616. structures efficiently.<br>
  617. <br>
  618.  
  619. </page>
  620. <page>
  621. Using <b>unions</b> conserve 
  622. storage. <br>
  623. <br>
  624.  
  625. </page>
  626. </section>
  627. <section type=Popup name=Code title="Code Examples">
  628. <page>
  629. <font size=18>Figure 18.2  Using variable-length argument lists.</font><br>
  630. <applet code=gsl.win.helper.TextBox width=580 height=300>
  631. <param  name="file" value="code/ch18/fig18_02.txt">
  632. </applet><br>
  633. <br>
  634. <foreign  name="copy2disk" url="!jcpy Source/fig18_02.jar">
  635. <foreign  name="audio" url="~audio/Ch18/18fig002.au">
  636. <foreign  name="execute" url="!jarexe Run/fig18_02.jar">
  637. <br>
  638.  
  639. </page>
  640. <page>
  641. <font size=18>Figure 18.3  Using command-line arguments.</font><br>
  642. Note: This program must be executed from the command line. Save to disk and run the 
  643. executable file from the directory it is saved in. You supply the program command line 
  644. arguments consisting of the name of the original file and the name of the file the contents of 
  645. the original file are to be copied to.<br>
  646. <applet code=gsl.win.helper.TextBox width=580 height=250>
  647. <param  name="file" value="code/ch18/fig18_03.txt">
  648. </applet><br>
  649. <br>
  650. <foreign  name="copy2disk" url="!jcpy Source/fig18_03.jar">
  651. <foreign  name="audio" url="~audio/Ch18/18fig003.au">
  652. <br>
  653.  
  654. </page>
  655. <page>
  656. <font size=18>Figure 18.4  Using functions <b>exit</b> and <b>atexit</b>.</font><br>
  657. <applet code=gsl.win.helper.TextBox width=580 height=300>
  658. <param  name="file" value="code/ch18/fig18_04.txt">
  659. </applet><br>
  660. <br>
  661. <foreign  name="copy2disk" url="!jcpy Source/fig18_04.jar">
  662. <foreign  name="audio" url="~audio/Ch18/18fig004.au">
  663. <foreign  name="execute" url="!jarexe Run/fig18_04.jar">
  664. <br>
  665.  
  666. </page>
  667. <page>
  668. <font size=18>Figure 18.6  Using signal handling.</font><br>
  669. <applet code=gsl.win.helper.TextBox width=580 height=300>
  670. <param  name="file" value="code/ch18/fig18_06.txt">
  671. </applet><br>
  672. <br>
  673. <foreign  name="copy2disk" url="!jcpy Source/fig18_06.jar">
  674. <foreign  name="audio" url="~audio/Ch18/18fig006.au">
  675. <foreign  name="execute" url="!jarexe Run/fig18_06.jar">
  676. <br>
  677.  
  678. </page>
  679. <page>
  680. <font size=18>Figure 18.7  Using <b>goto</b>.</font><br>
  681. <applet code=gsl.win.helper.TextBox width=580 height=300>
  682. <param  name="file" value="code/ch18/fig18_07.txt">
  683. </applet><br>
  684. <br>
  685. <foreign  name="copy2disk" url="!jcpy Source/fig18_07.jar">
  686. <foreign  name="audio" url="~audio/Ch18/18fig007.au">
  687. <foreign  name="execute" url="!jarexe Run/fig18_07.jar">
  688. <br>
  689.  
  690. </page>
  691. <page>
  692. <font size=18>Figure 18.8  Printing the value of a <b>union</b> in both member data types.</font><br>
  693. <applet code=gsl.win.helper.TextBox width=580 height=300>
  694. <param  name="file" value="code/ch18/fig18_08.txt">
  695. </applet><br>
  696. <br>
  697. <foreign  name="copy2disk" url="!jcpy Source/fig18_08.jar">
  698. <foreign  name="audio" url="~audio/Ch18/18fig008.au">
  699. <foreign  name="execute" url="!jarexe Run/fig18_08.jar">
  700. <br>
  701.  
  702. </page>
  703. <page>
  704. <font size=18>Figure 18.9  Using an anonymous <b>union</b>.</font><br>
  705. <applet code=gsl.win.helper.TextBox width=580 height=300>
  706. <param  name="file" value="code/ch18/fig18_09.txt">
  707. </applet><br>
  708. <br>
  709. <foreign  name="copy2disk" url="!jcpy Source/fig18_09.jar">
  710. <foreign  name="audio" url="~audio/Ch18/18fig009.au">
  711. <foreign  name="execute" url="!jarexe Run/fig18_09.jar">
  712. <br>
  713.  
  714. </page>
  715. </section>
  716. <section type=Popup name=Objective title="Objectives">
  717. <page>
  718. <indent width=8 delay>*   To be able to redirect keyboard input to come from a file and redirect 
  719. screen output into a file.</indent>
  720. <indent width=8 delay>*   To be able to write functions that use variable-length argument lists.</indent>
  721. <indent width=8 delay>*   To be able to process command-line arguments.</indent>
  722. <indent width=8 delay>*   To be able to process unexpected events within a program.</indent>
  723. <foreign  name="audio" url="~audio/Ch18/18obj.au">
  724.  
  725. </page>
  726. <page>
  727. <indent width=8 delay>*   To be able to allocate memory dynamically for arrays using C-style 
  728. dynamic memory allocation.</indent>
  729. <indent width=8 delay>*   To be able to resize memory dynamically allocated using C-style 
  730. dynamic memory allocation.</indent>
  731. <br>
  732.  
  733. </page>
  734. </section>
  735. <section type=Popup name=Engineer title="Engineering">
  736. <page>
  737. Global variables should 
  738. be avoided unless 
  739. application 
  740. performance is critical 
  741. because they violate the 
  742. principle of least 
  743. privilege, and they 
  744. make software difficult 
  745. to maintain.<br>
  746. <br>
  747.  
  748. </page>
  749. <page>
  750. Creating programs in 
  751. multiple source files 
  752. facilitates software 
  753. reusability and good 
  754. software engineering. 
  755. Functions may be 
  756. common to many 
  757. applications. In such 
  758. instances, those 
  759. functions should be <br>
  760.  
  761. </page>
  762. <page>
  763. stored in their own 
  764. source files, and each 
  765. source file should have 
  766. a corresponding header 
  767. file containing function 
  768. prototypes. This 
  769. enables programmers 
  770. of different applications 
  771. to reuse the same code 
  772. by including the proper <br>
  773.  
  774. </page>
  775. <page>
  776. header file, and 
  777. compiling their 
  778. application with the 
  779. corresponding source 
  780. file. <br>
  781. <br>
  782.  
  783. </page>
  784. <page>
  785. The <b>goto</b> statement 
  786. should be used only in 
  787. performance-oriented 
  788. applications. The <b>goto</b> 
  789. statement is 
  790. unstructured and can 
  791. lead to programs that 
  792. are more difficult to 
  793. debug, maintain, and 
  794. modify.<br>
  795.  
  796. </page>
  797. <page>
  798. As with a <b>struct</b> or a 
  799. <b>class</b> declaration, a 
  800. <b>union</b> declaration 
  801. simply creates a new 
  802. type. Placing a <b>union</b> 
  803. or <b>struct</b> declaration 
  804. outside any function 
  805. does not create a global 
  806. variable.<br>
  807. <br>
  808.  
  809. </page>
  810. </section>
  811. <section type=Popup name=Errors title="Common Errors">
  812. <page>
  813. Placing an ellipsis in 
  814. the middle of a function 
  815. parameter list. An 
  816. ellipsis may only be 
  817. placed at the end of the 
  818. parameter list.<br>
  819. <br>
  820.  
  821. </page>
  822. <page>
  823. The result of 
  824. referencing a <b>union</b> 
  825. member other than the 
  826. last one stored is 
  827. undefined. It treats the 
  828. stored data as the 
  829. different type.<br>
  830. <br>
  831.  
  832. </page>
  833. <page>
  834. Initializing a <b>union</b> in a 
  835. declaration with a value 
  836. or an expression whose 
  837. type is different from 
  838. the type of the <b>union</b>'s 
  839. first member.<br>
  840. <br>
  841.  
  842. </page>
  843. <page>
  844. Comparing <b>union</b>s is a 
  845. syntax error because 
  846. the compiler does not 
  847. know which member of 
  848. each is active, and 
  849. hence which member of 
  850. one to compare to 
  851. which member of the 
  852. other.<br>
  853. <br>
  854.  
  855. </page>
  856. </section>
  857. <section type=Body name=Default title="18 C Legacy Code Topics">
  858. <page>
  859. <font size=18 bold>18 C Legacy Code Topics</font><hr>
  860. <a href="#s1p0">18.1<spacer width=20 height=1>Introduction</a>  <br>
  861. <a href="#s2p0">18.2<spacer width=20 height=1>Redirecting Input/Output on UNIX and DOS 
  862. Systems</a>  <br>
  863. <a href="#s3p0">18.3<spacer width=20 height=1>Variable-Length Argument Lists</a>  <br>
  864. <a href="#s4p0">18.4<spacer width=20 height=1>Using Command-Line Arguments</a>  <br>
  865. <a href="#s5p0">18.5<spacer width=20 height=1>Notes on Compiling Multiple-Source-File 
  866. Programs</a>  <br>
  867. <a href="#s6p0">18.6<spacer width=20 height=1>Program Termination with Exit and Atexit</a>  <br>
  868. <a href="#s7p0">18.7<spacer width=20 height=1>The Volatile Type Qualifier</a>  <br>
  869. <foreign  name="objectivesButton" url="^Objective::c:s0p0">
  870. <foreign  name="quotes" url="^Quotes::c:s0p0">
  871.  
  872. </page>
  873. <page>
  874. <a href="#s8p0">18.8<spacer width=20 height=1>Suffixes for Integer and Floating-Point Constants</a>  <br>
  875. <a href="#s9p0">18.9<spacer width=20 height=1>Signal Handling</a>  <br>
  876. <a href="#s10p0">18.10<spacer width=20 height=1>Dynamic Memory Allocation: Functions <b>Calloc</b> 
  877. and <b>Realloc  
  878. </b></a><br>
  879. <a href="#s11p0">18.11<spacer width=20 height=1>The Unconditional Branch: Goto</a>  <br>
  880. <a href="#s12p0">18.12<spacer width=20 height=1>Unions</a>  <br>
  881. <a href="#s13p0">18.13<spacer width=20 height=1>Linkage Specifications</a>  <br>
  882. <a href="#s14p0">18.14<spacer width=20 height=1>Summary</a>  <br>
  883. <a href="^Terminology::c:s0p0">Terminology</a>  <br>
  884. <a href="^Illustration::c:s0p0">Figures</a>  <br>
  885. <foreign  name="objectivesButton" url="^Objective::c:s0p0">
  886. <foreign  name="quotes" url="^Quotes::c:s0p0">
  887.  
  888. </page>
  889. </section>
  890. <section type=Body name=Default title="18.1 Introduction">
  891. <page>
  892. <font size=18 bold>18.1 Introduction</font><hr>
  893. This chapter presents several advanced topics not 
  894. ordinarily covered in introductory courses. Many of the 
  895. capabilities discussed here are specific to particular 
  896. operating systems, especially UNIX and/or DOS. Much 
  897. of the material is for the benefit of C++ programmers 
  898. who will need to work with older C legacy code.<br>
  899.  
  900. </page>
  901. </section>
  902. <section type=Body name=Default title="18.2 Redirecting Input/Output on UNIX and DOS Systems">
  903. <page>
  904. <font size=18 bold>18.2 Redirecting Input/Output on UNIX and 
  905. DOS Systems</font><hr>
  906. Normally the input to a program is from the keyboard 
  907. (standard input), and the output from a program is 
  908. displayed on the screen (standard output). On most 
  909. computer systems--UNIX and DOS systems in 
  910. particular--it is possible to <i>redirect</i> inputs to come 
  911. from a file, and redirect outputs to be placed in a file. 
  912. Both forms of redirection can be accomplished without 
  913. using the file processing capabilities of the standard 
  914. library. <br>
  915.  
  916. </page>
  917. <page>
  918. There are several ways to redirect input and output from 
  919. the UNIX command line. Consider the executable file 
  920. sum that inputs integers one at a time and keeps a 
  921. running total of the values until the end-of-file indicator 
  922. is set, then prints the result. Normally the user inputs 
  923. integers from the keyboard and enters the end-of-file 
  924. key combination to indicate that no further values will 
  925. be input. With input redirection, the input can be stored 
  926. in a file. For example, if the data is stored in file <b>input</b>, 
  927. the command line<br>
  928. <font size=2><br></font><font size=11><pre>
  929. $ sum < input<p>
  930. </pre></font>
  931. causes program <b>sum</b> to be executed; the <i>redirect input 
  932. symbol (<b><</b>)</i> indicates that the data in file <b>input</b> (instead <br>
  933.  
  934. </page>
  935. <page>
  936. of the keyboard) is to be used as input by the program. 
  937. Redirecting input on a DOS system is performed 
  938. identically. <br>
  939. <spacer width=16 height=1>Note that <b>$</b> is the UNIX command line prompt (some 
  940. UNIX systems use a <b>%</b> prompt). Students often find it 
  941. difficult to understand that redirection is an operating 
  942. system function, not another C++ feature.<br>
  943. <spacer width=16 height=1>The second method of redirecting input is <i>piping</i>. A 
  944. <i>pipe (<b>|</b>)</i> causes the output of one program to be 
  945. redirected as the input to another program. Suppose 
  946. program <b>random</b> outputs a series of random integers; 
  947. the output of <b>random</b> can be "piped" directly to 
  948. program <b>sum</b> using the UNIX command line <br>
  949.  
  950. </page>
  951. <page>
  952. <font size=2><br></font><font size=11><pre>
  953. $ random | sum<p>
  954. </pre></font>
  955. This causes the sum of the integers produced by 
  956. <b>random</b> to be calculated. Piping can be performed in 
  957. UNIX and DOS.<br>
  958. <spacer width=16 height=1>Program output can be redirected to a file by using the 
  959. <i>redirect output symbol (<b>></b>)</i> (the same symbol is used for 
  960. UNIX and DOS). For example, to redirect the output of 
  961. program <b>random</b> to file <b>out</b>, use <br>
  962. <font size=2><br></font><font size=11><pre>
  963. $ random > out<p>
  964. </pre></font>
  965. Finally, program output can be appended to the end of 
  966. an existing file by using the <i>append output symbol (<b>>></b>)</i> 
  967. (the same symbol is used for UNIX and DOS). For 
  968. example, to append the output from program <b>random</b> <br>
  969.  
  970. </page>
  971. <page>
  972. to file <b>out</b> created in the preceding command line, use 
  973. the command line<br>
  974. <font size=2><br></font><font size=11><pre>
  975. $ random >> out<p>
  976. </pre></font>
  977.  
  978. </page>
  979. </section>
  980. <section type=Body name=Default title="18.3 Variable-Length Argument Lists">
  981. <page>
  982. <font size=18 bold>18.3 Variable-Length Argument Lists</font><hr>
  983. [Note: This material is included for the benefit of C++ 
  984. programmers who will work with C legacy code. In 
  985. C++, programmers use function overloading to 
  986. accomplish much of what C programmers accomplish 
  987. with variable-length argument lists.] It is possible to 
  988. create functions that receive an unspecified number of 
  989. arguments. <a href="^Errors::c:s0p0"><img src="bckgrnds/icons/cpe_ico.gif" align=sidebar></a>An ellipsis (<b>...</b>) in a function's prototype 
  990. indicates that the function receives a variable number of 
  991. arguments of any type. Note that the ellipsis must 
  992. always be placed at the end of the parameter list, and 
  993. there must be at least one named parameter. The macros <br>
  994.  
  995. </page>
  996. <page>
  997. and definitions of the <i>variable arguments header 
  998. <tt></tt><b>stdarg.h </b></i>(<a href="^Illustration::c:s0p1"><img src="bckgrnds/icons/ill_ico.gif" align=sidebar>Fig. 18.1</a>) provide the capabilities necessary 
  999. to build functions with variable-length argument lists. <br>
  1000. <spacer width=16 height=1>The program of <a href="^Code::c:s0p0"><img src="bckgrnds/icons/code_ico.gif" align=sidebar>Fig. 18.2</a> demonstrates function 
  1001. <b>average</b> that receives a variable number of arguments. 
  1002. The first argument of <b>average</b> is always the number of 
  1003. values to be averaged. <br>
  1004. <spacer width=16 height=1>Function <b>average</b> uses all the definitions and macros of 
  1005. header <b>stdarg.h</b>. Object <b>ap</b>, of type <b>va_list</b>, is used by 
  1006. macros <b>va_start</b>, <b>va_arg</b>, and <b>va_end</b> to process the 
  1007. variable-length argument list of function <b>average</b>. The 
  1008. function begins by invoking <b>va_start</b> to initialize object 
  1009. <b>ap</b> for use in <b>va_arg</b> and <b>va_end</b>. The macro receives <br>
  1010.  
  1011. </page>
  1012. <page>
  1013. two arguments--object <b>ap</b> and the identifier of the 
  1014. rightmost argument in the argument list before the 
  1015. ellipsis--<b>i</b> in this case (<b>va_start</b> uses <b>i</b> here to 
  1016. determine where the variable-length argument list 
  1017. begins). Next, function <b>average</b> repeatedly adds the 
  1018. arguments in the variable-length argument list to 
  1019. variable <b>total</b>. The value to be added to <b>total</b> is 
  1020. retrieved from the argument list by invoking macro 
  1021. <b>va_arg</b>. Macro <b>va_arg</b> receives two arguments--object 
  1022. <b>ap</b>, and the type of the value expected in the argument 
  1023. list--<b>double</b> in this case. The macro returns the value 
  1024. of the argument. Function <b>average</b> invokes macro 
  1025. <b>va_end</b> with object <b>ap</b> as an argument to facilitate a <br>
  1026.  
  1027. </page>
  1028. <page>
  1029. normal return to <b>main</b> from <b>average</b>. Finally, the 
  1030. average is calculated and returned to <b>main</b>. Note that 
  1031. we used only double arguments for the variable-length 
  1032. portion of the argument list. Actually, any data type or a 
  1033. mixture of data types can be used as long as the proper 
  1034. type is specified each time <b>va_arg</b> is used.<br>
  1035.  
  1036. </page>
  1037. <page>
  1038. <b>Drag the correct identifier to the box associated with 
  1039. the identifier:</b><br>
  1040. <component type="drag" width=56 height=18 label="va_list" name="va_list">   <component type="drag" width=64 height=18 label="va_start" name="va_start">   <component type="drag" width=48 height=18 label="va_arg" name="va_arg">   <component type="drag" width=48 height=18 label="va_end" name="va_end"> <br>
  1041. A macro that is invoked before accessing a variable-length argument list.<component type="drop" width=72 height=18 name="va_start">  <br>
  1042. A macro that facilitates normal return from a function with a variable length argument list.<component type="drop" width=72 height=18 name="va_end">  <br>
  1043. To access the arguments in a variable length argument list, a maco of this type must be created.<component type="drop" width=72 height=18 name="va_list">  <br>
  1044. A macro that expands to an expression of the value and type of the next argument.<component type="drop" width=72 height=18 name="va_arg">  <br>
  1045. <component type=button name=b label="Check Your Answer" width=125 height=24>
  1046.  
  1047. </page>
  1048. </section>
  1049. <section type=Body name=Default title="18.4 Using Command-Line Arguments">
  1050. <page>
  1051. <font size=18 bold>18.4 Using Command-Line Arguments</font><hr>
  1052. On many systems--DOS and UNIX in particular--it is 
  1053. possible to pass arguments to <b>main</b> from a command 
  1054. line by including parameters <b>int</b> <b>argc</b> and <b>char</b> <b>*argv[]</b> 
  1055. in the parameter list of <b>main</b>. Parameter <b>argc</b> receives 
  1056. the number of command-line arguments. Parameter 
  1057. <b>argv</b> is an array of strings in which the actual 
  1058. command-line arguments are stored. Common uses of 
  1059. command-line arguments include printing the 
  1060. arguments, passing options to a program, and passing 
  1061. filenames to a program. <br>
  1062.  
  1063. </page>
  1064. <page>
  1065. The program of<a href="^Code::c:s0p1"> <img src="bckgrnds/icons/code_ico.gif" align=sidebar>Fig. 18.3</a> copies a file into another file 
  1066. one character at a time. The executable file for the 
  1067. program is called <b>copy</b>. A typical command line for the 
  1068. <b>copy</b> program on a UNIX system is<br>
  1069. <font size=2><br></font><font size=11><pre>
  1070. $ copy input output<p>
  1071. </pre></font>
  1072. This command line indicates that file <b>input</b> is to be 
  1073. copied to file <b>output</b>. When the program executes, if 
  1074. <b>argc</b> is not <b>3</b> (<b>copy</b> counts as one of the arguments), the 
  1075. program prints an error message and terminates. 
  1076. Otherwise, array <b>argv</b> contains the strings "<b>copy</b>", 
  1077. "<b>input</b>" and "<b>output</b>". The second and third 
  1078. arguments on the command line are used as file names 
  1079. by the program. The files are opened by creating <br>
  1080.  
  1081. </page>
  1082. <page>
  1083. <b>ifstream</b> object <b>inFile</b> and <b>ofstream</b> object <b>outFile</b>. If 
  1084. both files are opened successfully, characters are read 
  1085. from file <b>input</b> with member function <b>get</b> and written 
  1086. to file <b>output</b> with member function <b>put</b> until the end-
  1087. of-file indicator for file <b>input</b> is set. Then the program 
  1088. terminates. The result is an exact copy of file <b>input</b>. 
  1089. Note that not all computer systems support command-
  1090. line arguments as easily as UNIX and DOS. Macintosh 
  1091. and VMS systems, for example, require special settings 
  1092. for processing command-line arguments. See the 
  1093. manuals for your system for more information on 
  1094. command-line arguments.<br>
  1095.  
  1096. </page>
  1097. <page>
  1098. <b>Drag the correct identifier to the box associated with 
  1099. the identifier:</b><br>
  1100. <component type="drag" width=32 height=18 label="argc" name="argc">   <component type="drag" width=32 height=18 label="argv" name="argv">   <br>
  1101. The name of the argument to main that is an array of strings representing the command-line arguments.<component type="drop" width=40 height=18 name="argv">  <br>
  1102. The name of the argument that represents the number of command-line arguments passed to main.<component type="drop" width=40 height=18 name="argc">  <br>
  1103. <component type=button name=b label="Check Your Answer" width=125 height=24>
  1104.  
  1105. </page>
  1106. </section>
  1107. <section type=Body name=Default title="18.5 Notes on Compiling Multiple-Source-File Programs">
  1108. <page>
  1109. <font size=18 bold>18.5 Notes on Compiling Multiple-Source-
  1110. File Programs</font><hr>
  1111. As stated earlier in the text, it is possible to build 
  1112. programs that consist of multiple source files (see 
  1113. Chapter 6, "Classes and Data Abstraction"). There are 
  1114. several considerations when creating programs in 
  1115. multiple files. For example, the definition of a function 
  1116. must be entirely contained in one file--it cannot span 
  1117. two or more files. <br>
  1118. <spacer width=16 height=1>In Chapter 3, we introduced the concepts of storage 
  1119. class and scope. We learned that variables declared 
  1120. outside any function definition are of storage class <br>
  1121.  
  1122. </page>
  1123. <page>
  1124. <b>static</b> by default and are referred to as global variables. 
  1125. Global variables are accessible to any function defined 
  1126. in the same file after the variable is declared. Global 
  1127. variables also are accessible to functions in other files, 
  1128. however, the global variables must be declared in each 
  1129. file in which they are used. For example, if we define 
  1130. global integer variable flag in one file, and refer to it 
  1131. in a second file, the second file must contain the 
  1132. declaration <br>
  1133. <font size=2><br></font><font size=11><pre>
  1134. extern int flag;<p>
  1135. </pre></font>
  1136. prior to the variable's use in that file. In the preceding 
  1137. declaration, the storage class specifier extern 
  1138. indicates to the compiler that variable flag is defined <br>
  1139.  
  1140. </page>
  1141. <page>
  1142. either later in the same file or in a different file. The 
  1143. compiler informs the linker that unresolved references 
  1144. to variable <b>flag</b> appear in the file (the compiler does not 
  1145. know where the <b>flag</b> is defined, so it lets the linker 
  1146. attempt to find <b>flag</b>). If the linker cannot locate a 
  1147. definition of <b>flag</b>, a linker error is reported, and no 
  1148. executable file is produced. <a href="^Perform::c:s0p0"><img src="bckgrnds/icons/perf_ico.gif" align=sidebar></a>If a proper global definition 
  1149. is located, the linker resolves the references by 
  1150. indicating where <b>flag</b> is located. <br>
  1151. <spacer width=16 height=1><a href="^Engineer::c:s0p0"><img src="bckgrnds/icons/seo_ico.gif" align=sidebar></a>Just as <b>extern</b> declarations can be used to declare global 
  1152. variables to other program files, function prototypes 
  1153. can extend the scope of a function beyond the file in 
  1154. which it is defined (the <b>extern</b> specifier is not required <br>
  1155.  
  1156. </page>
  1157. <page>
  1158. in a function prototype). <a href="^Portable::c:s0p0"><img src="bckgrnds/icons/port_ico.gif" align=sidebar></a>This is accomplished by 
  1159. including the function prototype in each file in which 
  1160. the function is invoked, and compiling the files together 
  1161. (see Section 17.2). Function prototypes indicate to the 
  1162. compiler that the specified function is defined either 
  1163. later in the same file or in a different file. The compiler 
  1164. does not attempt to resolve references to such a 
  1165. function--that task is left to the linker. If the linker 
  1166. cannot locate a function definition, an error is 
  1167. generated. <br>
  1168. <spacer width=16 height=1>As an example of using function prototypes to extend 
  1169. the scope of a function, consider any program 
  1170. containing the preprocessor directive <b>#include</b> <br>
  1171.  
  1172. </page>
  1173. <page>
  1174. <b><string.h></b>. This directive includes in a file the function 
  1175. prototypes for functions such as <b>strcmp</b> and <b>strcat</b>. 
  1176. Other functions in the file can use <b>strcmp</b> and <b>strcat</b> to 
  1177. accomplish their tasks. The <b>strcmp</b> and <b>strcat</b> 
  1178. functions are defined for us separately. We do not need 
  1179. to know where they are defined. <a href="^Engineer::c:s0p1"><img src="bckgrnds/icons/seo_ico.gif" align=sidebar></a>We are simply reusing 
  1180. the code in our programs. The linker resolves our 
  1181. references to these functions automatically. This 
  1182. process enables us to use the functions in the standard 
  1183. library.<br>
  1184. <spacer width=16 height=1>It is possible to restrict the scope of a global variable or 
  1185. function to the file in which it is defined. The storage 
  1186. class specifier <b>static</b>, when applied to a global variable <br>
  1187.  
  1188. </page>
  1189. <page>
  1190. or a function, prevents it from being used by any 
  1191. function that is not defined in the same file. This is 
  1192. referred to as <i>internal linkage</i>. Global variables and 
  1193. functions that are not preceded by <b>static</b> in their 
  1194. definitions have <i>external linkage</i>--they can be accessed 
  1195. in other files if those files contain proper declarations 
  1196. and/or function prototypes. <br>
  1197. <spacer width=16 height=1>The global variable declaration <br>
  1198. <font size=2><br></font><font size=11><pre>
  1199. static float pi = 3.14159;<p>
  1200. </pre></font>
  1201. creates variable <b>pi</b> of type <b>float</b>, initializes it to 
  1202. <b>3.14159</b>, and indicates that <b>pi</b> is known only to 
  1203. functions in the file in which it is defined. <br>
  1204.  
  1205. </page>
  1206. <page>
  1207. The <b>static</b> specifier is commonly used with utility 
  1208. functions that are called only by functions in a 
  1209. particular file. If a function is not required outside a 
  1210. particular file, the principle of least privilege should be 
  1211. enforced by using <b>static</b>. If a function is defined before 
  1212. it is used in a file, <b>static</b> should be applied to the 
  1213. function definition. Otherwise, <b>static</b> should be applied 
  1214. to the function prototype.<br>
  1215. <spacer width=16 height=1>When building large programs in multiple source files, 
  1216. compiling the program becomes tedious if small 
  1217. changes are made to one file, and the entire program 
  1218. must be recompiled. Many systems provide special 
  1219. utilities that recompile only the modified program file. <br>
  1220.  
  1221. </page>
  1222. <page>
  1223. On UNIX systems the utility is called <b><i>make</i></b>. Utility 
  1224. <b>make</b> reads a file called <b><i>makefile</i></b> that contains 
  1225. instructions for compiling and linking the program. 
  1226. Systems such as Borland C++ and Microsoft Visual 
  1227. C++ for PCs provide make utilities and "projects". For 
  1228. more information on <b>make</b> utilities, see the manual for 
  1229. your particular system.<br>
  1230.  
  1231. </page>
  1232. <page>
  1233. <b>Select the true statement(s). </b><br>
  1234. <component type="checkbox" width=20 height=18 label="" name=""  correct="True.">
  1235. Functions and global variables have internal linkage if they are declared as static.  <br>
  1236. <component type="checkbox" width=20 height=18 label="" name=""  correct="True.">
  1237. Unix utility make reads a file called a makefile that contains instructions for compiling and linking multiple source files.  <br>
  1238. <component type="checkbox" width=20 height=18 label="" name=""  feedback="False. Global variables are accessible to other files, but must be declared with storage class specifier extern in the files that do not define the global variable.">
  1239. Global variables are accessible to other files in a programwithout the need for a storage class specifier.   <br>
  1240. <component type=button name=b label="Check Your Answer" width=125 height=24>
  1241.  
  1242. </page>
  1243. </section>
  1244. <section type=Body name=Default title="18.6 Program Termination with Exit and Atexit">
  1245. <page>
  1246. <font size=18 bold>18.6 Program Termination with Exit and 
  1247. Atexit</font><hr>
  1248. The general utilities library (<b>stdlib.h</b>) provides methods 
  1249. of terminating program execution other than a 
  1250. conventional return from function <b>main</b>. Function <b><i>exit</i></b> 
  1251. forces a program to terminate as if it executed normally. 
  1252. The function often is used to terminate a program when 
  1253. an error is detected in the input, or if a file to be 
  1254. processed by the program cannot be opened. Function 
  1255. <b><i>atexit registers</i></b> a function in the program to be called 
  1256. upon successful termination of the program--i.e., either <br>
  1257.  
  1258. </page>
  1259. <page>
  1260. when the program terminates by reaching the end of 
  1261. <b>main</b>, or when <b>exit</b> is invoked. <br>
  1262. <spacer width=16 height=1>Function <b>atexit</b> takes a pointer to a function (i.e., the 
  1263. function name) as an argument. Functions called at 
  1264. program termination cannot have arguments, and 
  1265. cannot return a value. Up to 32 functions may be 
  1266. registered for execution at program termination. <br>
  1267. <spacer width=16 height=1>Function <b>exit</b> takes one argument. The argument is 
  1268. normally the symbolic constant <b>EXIT_SUCCESS</b> or 
  1269. the symbolic constant <b>EXIT_FAILURE</b>. If <b>exit</b> is 
  1270. called with <b>EXIT_SUCCESS</b>, the implementation-
  1271. defined value for successful termination is returned to 
  1272. the calling environment. If <b>exit</b> is called with <br>
  1273.  
  1274. </page>
  1275. <page>
  1276. <b>EXIT_FAILURE</b>, the implementation-defined value 
  1277. for unsuccessful termination is returned. When function 
  1278. <b>exit</b> is invoked, any functions previously registered 
  1279. with <b>atexit</b> are invoked in the reverse order of their 
  1280. registration, all streams associated with the program are 
  1281. flushed and closed, and control returns to the host 
  1282. environment. The program of <a href="^Code::c:s0p2"><img src="bckgrnds/icons/code_ico.gif" align=sidebar>Fig. 18.4</a> tests functions 
  1283. <b>exit</b> and <b>atexit</b>. The program prompts the user to 
  1284. determine whether the program should be terminated 
  1285. with <b>exit</b> or by reaching the end of <b>main</b>. Note that 
  1286. function <b>print</b> is executed at program termination in 
  1287. each case.<br>
  1288.  
  1289. </page>
  1290. <page>
  1291. <b>Select the true statement(s). </b><br>
  1292. <component type="checkbox" width=20 height=18 label="" name=""  feedback="False. The functions cannot take any arguments.">
  1293. Functions that are registered to be called at termination by atexit take a string argument which is displayed.   <br>
  1294. <component type="checkbox" width=20 height=18 label="" name=""  correct="True.">
  1295. Function exit normally takes either EXIT_FAILURE or EXIT_SUCCESS as an argument.  <br>
  1296. <component type=button name=b label="Check Your Answer" width=125 height=24>
  1297.  
  1298. </page>
  1299. </section>
  1300. <section type=Body name=Default title="18.7 The Volatile Type Qualifier">
  1301. <page>
  1302. <font size=18 bold>18.7 The Volatile Type Qualifier</font><hr>
  1303. The <b><i>volatile</i></b> type qualifier is applied to a definition of a 
  1304. variable that may be altered from outside the program 
  1305. (i.e., the variable is not completely under the control of 
  1306. the program). Thus, the compiler cannot perform 
  1307. optimizations (such as speeding program execution or 
  1308. reducing memory consumption, for example) that 
  1309. depend on "knowing a variable's behavior is influenced 
  1310. only by program activities the compiler can observe." <br>
  1311.  
  1312. </page>
  1313. </section>
  1314. <section type=Body name=Default title="18.8 Suffixes for Integer and Floating-Point Constants">
  1315. <page>
  1316. <font size=18 bold>18.8 Suffixes for Integer and Floating-Point 
  1317. Constants</font><hr>
  1318. C++ provides integer and floating-point suffixes for 
  1319. specifying the types of integer and floating-point 
  1320. constants. The integer suffixes are: <b>u</b> or <b>U</b> for an 
  1321. <b>unsigned</b> integer, <b>l</b> or <b>L</b> for a <b>long</b> integer, and <b>ul</b> or <b>UL</b> 
  1322. for an <b>unsigned</b> <b>long</b> integer. The following constants 
  1323. are of type <b>unsigned</b>, <b>long</b>, and <b>unsigned long</b>, 
  1324. respectively:<br>
  1325. <font size=2><br></font><font size=11><pre>
  1326. 174u<p>
  1327. 8358L<p>
  1328. 28373ul<p>
  1329. </pre></font>
  1330.  
  1331. </page>
  1332. <page>
  1333. If an integer constant is not suffixed, its type is 
  1334. determined by the first type capable of storing a value 
  1335. of that size (first <b>int</b>, then l<b>ong int</b>, then <b>unsigned long 
  1336. int</b>).<br>
  1337. <spacer width=16 height=1>The floating-point suffixes are: <b>f</b> or <b>F</b> for a <b>float</b>, and <b>l</b> 
  1338. or <b>L</b> for a <b>long double</b>. The following constants are of 
  1339. type <b>long double</b> and <b>float</b>, respectively:<br>
  1340. <font size=2><br></font><font size=11><pre>
  1341. 3.14159L<p>
  1342. 1.28f<p>
  1343. </pre></font>
  1344. A floating-point constant that is not suffixed is 
  1345. automatically of type <b>double</b>.<br>
  1346.  
  1347. </page>
  1348. <page>
  1349. <b>Drag the correct suffix to the box associated with the 
  1350. suffix:</b><br>
  1351.  <component type="drag" width=16 height=18 label="ul" name="ul">   <component type="drag" width=12 height=18 label="L" name="L">   <component type="drag" width=8 height=18 label="u" name="u">   <component type="drag" width=8 height=18 label="f" name="f">   <br>
  1352. Suffix that indicates an unsigned int constant.<component type="drop" width=24 height=18 name="u">  <br>
  1353. Suffix that indicates a float constant.<component type="drop" width=24 height=18 name="f">  <br>
  1354. Suffix that indicates an unsigned long constant.<component type="drop" width=24 height=18 name="ul">  <br>
  1355. Suffix that indicates a long constant.<component type="drop" width=24 height=18 name="L">  <br>
  1356. <component type=button name=b label="Check Your Answer" width=125 height=24>
  1357.  
  1358. </page>
  1359. </section>
  1360. <section type=Body name=Default title="18.9 Signal Handling">
  1361. <page>
  1362. <font size=18 bold>18.9 Signal Handling</font><hr>
  1363. An unexpected event, or <i>signal</i>, can terminate a 
  1364. program prematurely. Some unexpected events include 
  1365. <i>interrupts</i> (typing <b><ctrl></b> <b>c</b> on a UNIX or DOS system), 
  1366. <i>illegal instructions</i>, <i>segmentation violations</i>, 
  1367. <i>termination orders from the operating system</i>, and 
  1368. <i>floating-point exceptions</i> (division by zero or 
  1369. multiplying large floating-point values). The <i>signal 
  1370. handling library</i> provides function <b><i>signal</i></b> to <i>trap</i> 
  1371. unexpected events. Function <b>signal</b> receives two 
  1372. arguments--an integer signal number and a pointer to 
  1373. the signal handling function. Signals can be generated <br>
  1374.  
  1375. </page>
  1376. <page>
  1377. by function <b><i>raise</i></b> which takes an integer signal number 
  1378. as an argument. <a href="^Illustration::c:s0p2"><img src="bckgrnds/icons/ill_ico.gif" align=sidebar>Figure 18.5</a> summarizes the standard 
  1379. signals defined in header file <b>signal.h.</b> The program of 
  1380. <a href="^Code::c:s0p3"><img src="bckgrnds/icons/code_ico.gif" align=sidebar>Fig. 18.6</a> demonstrates functions <b>signal</b> and <b>raise</b>.<br>
  1381. <spacer width=16 height=1><a href="^Code::c:s0p3"><img src="bckgrnds/icons/code_ico.gif" align=sidebar>Figure 18.6</a> traps an interactive signal (<b>SIGINT</b>) with 
  1382. function <b>signal</b>. The program calls <b>signal</b> with <b>SIGINT</b> 
  1383. and a pointer to function <b>signal_handler</b> (remember 
  1384. that the name of a function is a pointer to the the 
  1385. function). Now, when a signal of type <b>SIGINT</b> occurs, 
  1386. function <b>signal_handler</b> is called, a message is printed, 
  1387. and the user is given the option to continue normal 
  1388. execution of the program. If the user wishes to continue 
  1389. execution, the signal handler is reinitialized by calling <br>
  1390.  
  1391. </page>
  1392. <page>
  1393. <b>signal</b> again (some systems require the signal handler to 
  1394. be reinitialized), and control returns to the point in the 
  1395. program at which the signal was detected. In this 
  1396. program, function <b>raise</b> is used to simulate an 
  1397. interactive signal. A random number between <b>1</b> and <b>50</b> 
  1398. is chosen. If the number is <b>25</b>, then <b>raise</b> is called to 
  1399. generate the signal. Normally, interactive signals are 
  1400. initiated outside the program. For example, typing 
  1401. <b><ctrl> c</b> during program execution on a UNIX or DOS 
  1402. system generates an interactive signal that terminates 
  1403. program execution. Signal handling can be used to trap 
  1404. the interactive signal and prevent the program from 
  1405. terminating.<br>
  1406.  
  1407. </page>
  1408. <page>
  1409. <b>Drag the correct term to the box associated with the 
  1410. term:</b><br>
  1411. <component type="drag" width=64 height=18 label="signal.h" name="signal.h">   <component type="drag" width=48 height=18 label="signal" name="signal">   <component type="drag" width=40 height=18 label="raise" name="raise">   <component type="drag" width=72 height=18 label="SIGINT" name="SIGINT">   <component type="drag" width=88 height=18 label="interrupt.h" name="interrupt.h">   <br>
  1412. Function that can be invoked to signal the program.<component type="drop" width=80 height=18 name="raise">  <br>
  1413. Header file to include to use the signal and raise capabilities.<component type="drop" width=80 height=18 name="signal.h">  <br>
  1414. Function called to register a signal handler.<component type="drop" width=80 height=18 name="signal">  <br>
  1415. Symbolic constant that represents an interrupt signal.<component type="drop" width=80 height=18 name="SIGINT">  <br>
  1416. <component type=button name=b label="Check Your Answer" width=125 height=24>
  1417.  
  1418. </page>
  1419. </section>
  1420. <section type=Body name=Default title="18.10 Dynamic Memory Allocation: Functions Calloc and Realloc">
  1421. <page>
  1422. <font size=18 bold>18.10 Dynamic Memory Allocation: Functions 
  1423. <b>Calloc</b> and <b>Realloc
  1424. </b></font><hr>
  1425. In Chapter 7, when we discussed C++-style dynamic 
  1426. memory allocation with <b>new</b> and <b>delete</b>, we compared 
  1427. <b>new</b> and <b>delete</b> with the C functions <b>malloc</b> and <b>free</b>. 
  1428. C++ programmers should use <b>new</b> and <b>delete</b> not 
  1429. <b>malloc</b> and <b>free</b>. However, most C++ programmers will 
  1430. find themselves reading a great deal of C legacy code, 
  1431. and therefore we include this additional discussion on 
  1432. C-style dynamic memory allocation.<br>
  1433. <spacer width=16 height=1>The general utilities library (<b>stdlib.h</b>) provides two 
  1434. other functions for dynamic memory allocation--<b>calloc</b> <br>
  1435.  
  1436. </page>
  1437. <page>
  1438. and <b>realloc</b>. These functions can be used to create and 
  1439. modify <i>dynamic arrays</i>. As shown in Chapter 5, 
  1440. "Pointers and Strings," a pointer to an array can be 
  1441. subscripted like an array. Thus, a pointer to a 
  1442. contiguous portion of memory created by <b>calloc</b> can be 
  1443. manipulated as an array. Function <b>calloc</b> dynamically 
  1444. allocates memory for an array. The prototype for <b>calloc</b> 
  1445. is <br>
  1446. <font size=2><br></font><font size=11><pre>
  1447. void *calloc( size_t nmemb, size_t size );<p>
  1448. </pre></font>
  1449. It receives two arguments--the number of elements 
  1450. (<b>nmemb</b>) and the size of each element (<b>size</b>)--and 
  1451. initializes the elements of the array to zero. The <br>
  1452.  
  1453. </page>
  1454. <page>
  1455. function returns a pointer to the allocated memory, or a 
  1456. null pointer (<b>0</b>) if the memory is not allocated. <br>
  1457. <spacer width=16 height=1>Function <b>realloc</b> changes the size of an object allocated 
  1458. by a previous call to <b>malloc</b>, <b>calloc</b> or <b>realloc</b>. The 
  1459. original object's contents are not modified provided that 
  1460. the memory allocated is larger than the amount 
  1461. allocated previously. Otherwise, the contents are 
  1462. unchanged up to the size of the new object. The 
  1463. prototype for <b>realloc</b> is <br>
  1464. <font size=2><br></font><font size=11><pre>
  1465. void *realloc( void *ptr, size_t size );<p>
  1466. </pre></font>
  1467. Function <b>realloc</b> takes two arguments--a pointer to the 
  1468. original object (<b>ptr</b>) and the new size of the object 
  1469. (<b>size</b>). If <b>ptr</b> is <b>0</b>, <b>realloc</b> works identically to <b>malloc</b>. If <br>
  1470.  
  1471. </page>
  1472. <page>
  1473. <b>size</b> is <b>0</b> and <b>ptr</b> is not <b>0</b>, the memory for the object is 
  1474. freed. Otherwise, if <b>ptr</b> is not <b>0</b> and size is greater than 
  1475. zero, <b>realloc</b> tries to allocate a new block of memory 
  1476. for the object. If the new space cannot be allocated, the 
  1477. object pointed to by <b>ptr</b> is unchanged. Function <b>realloc</b> 
  1478. returns either a pointer to the reallocated memory, or a 
  1479. null pointer.<br>
  1480.  
  1481. </page>
  1482. <page>
  1483. <b>Select the C++ statement that does not generate an 
  1484. error. </b><br>
  1485. <component type="checkbox" width=20 height=18 label="" name=""  correct="Correct.">
  1486. char *cPtr=static_cast<char *>(calloc(3, 1));.  <br>
  1487. <component type="checkbox" width=20 height=18 label="" name=""  feedback="Incorrect.">
  1488. char cPtr = calloc(3, 1);.   <br>
  1489. <component type="checkbox" width=20 height=18 label="" name=""  feedback="Incorrect.">
  1490. char *cPtr = static_cast<char>( calloc( 3 ) ).   <br>
  1491. <component type="checkbox" width=20 height=18 label="" name=""  feedback="Incorrect.">
  1492. char *cPtr = calloc(sizeof( char * ) * 3);.   <br>
  1493. <component type=button name=b label="Check Your Answer" width=125 height=24>
  1494.  
  1495. </page>
  1496. </section>
  1497. <section type=Body name=Default title="18.11 The Unconditional Branch: Goto">
  1498. <page>
  1499. <font size=18 bold>18.11 The Unconditional Branch: Goto</font><hr>
  1500. Throughout the text we have stressed the importance of 
  1501. using structured programming techniques to build 
  1502. reliable software that is easy to debug, maintain, and 
  1503. modify. In some cases, performance is more important 
  1504. than strict adherence to structured programming 
  1505. techniques. In these cases, some unstructured 
  1506. programming techniques may be used. For example, we 
  1507. can use <b>break</b> to terminate execution of a repetition 
  1508. structure before the loop continuation condition 
  1509. becomes false. This saves unnecessary repetitions of <br>
  1510.  
  1511. </page>
  1512. <page>
  1513. the loop if the task is completed before loop 
  1514. termination. <br>
  1515. <spacer width=16 height=1>Another instance of unstructured programming is the 
  1516. <b><i>goto statement</i></b>--an unconditional branch. The result of 
  1517. the <b>goto</b> statement is a change in the flow of control of 
  1518. the program to the first statement after the <i>label</i> 
  1519. specified in the <b>goto</b> statement. A label is an identifier 
  1520. followed by a colon. A label must appear in the same 
  1521. function as the <b>goto</b> statement that refers to it. The 
  1522. program of <a href="^Code::c:s0p4"><img src="bckgrnds/icons/code_ico.gif" align=sidebar>Fig. 18.7</a> uses <b>goto</b> statements to loop ten 
  1523. times and print the counter value each time. After 
  1524. initializing <b>count</b> to <b>1</b>, the program tests <b>count</b> to 
  1525. determine whether it is greater than <b>10</b> (the label <b>start</b> <br>
  1526.  
  1527. </page>
  1528. <page>
  1529. is skipped because labels do not perform any action). If 
  1530. so, control is transferred from the <b>goto</b> to the first 
  1531. statement after the label <b>end</b>. Otherwise, <b>count</b> is 
  1532. printed and incremented, and control is transferred from 
  1533. the <b>goto</b> to the first statement after the label <b>start</b>. <br>
  1534. <spacer width=16 height=1>In Chapter 2, we stated that only three control structures 
  1535. are required to write any program--sequence, selection, 
  1536. and repetition. When the rules of structured 
  1537. programming are followed, it is possible to create 
  1538. deeply nested control structures from which it is 
  1539. difficult to efficiently escape. <a href="^Perform::c:s0p1"><img src="bckgrnds/icons/perf_ico.gif" align=sidebar></a>Some programmers use 
  1540. <b>goto</b> statements in such situations as a quick exit from a <br>
  1541.  
  1542. </page>
  1543. <page>
  1544. deeply nested structure. <a href="^Engineer::c:s0p4"><img src="bckgrnds/icons/seo_ico.gif" align=sidebar></a>This eliminates the need to test 
  1545. multiple conditions to escape from a control structure.<br>
  1546.  
  1547. </page>
  1548. <page>
  1549. <b>Select the statement that does not generate an error. </b><br>
  1550. <component type="checkbox" width=20 height=18 label="" name=""  feedback="Incorrect.">
  1551. goto (x).   <br>
  1552. <component type="checkbox" width=20 height=18 label="" name=""  correct="Correct.">
  1553. goto x;.  <br>
  1554. <component type="checkbox" width=20 height=18 label="" name=""  feedback="Incorrect.">
  1555. goto x:.   <br>
  1556. <component type="checkbox" width=20 height=18 label="" name=""  feedback="Incorrect.">
  1557. goto x:;.   <br>
  1558. <component type=button name=b label="Check Your Answer" width=125 height=24>
  1559.  
  1560. </page>
  1561. </section>
  1562. <section type=Body name=Default title="18.12 Unions">
  1563. <page>
  1564. <font size=18 bold>18.12 Unions</font><hr>
  1565. A <b>union</b> (defined with keyword <b>union</b>) is a region of 
  1566. memory that, over time, can contain objects of a variety 
  1567. of types. However, at any moment, a <b>union</b> can contain 
  1568. a maximum of one object because the members of a 
  1569. <b>union</b> share the same storage space. <a href="^Errors::c:s0p1"><img src="bckgrnds/icons/cpe_ico.gif" align=sidebar></a>It is the 
  1570. programmer's responsibility to ensure that the data in a 
  1571. <b>union</b> is <a href="^Portable::c:s0p1"><img src="bckgrnds/icons/port_ico.gif" align=sidebar></a>referenced with a member name of the proper 
  1572. data type.<br>
  1573. <spacer width=16 height=1>At different times during a program's execution, some 
  1574. objects may not be relevant, but one other object is--so 
  1575. a <b>union</b> <b><a href="^Perform::c:s0p2"><img src="bckgrnds/icons/perf_ico.gif" align=sidebar></a></b>shares the space instead of wasting storage on <br>
  1576.  
  1577. </page>
  1578. <page>
  1579. objects that are not being used. The number of bytes 
  1580. used to <a href="^Portable::c:s0p3"><img src="bckgrnds/icons/port_ico.gif" align=sidebar></a>store a <b>union</b> must be at least enough to hold 
  1581. the largest member. <br>
  1582. <spacer width=16 height=1> <a href="^Portable::c:s0p2"><img src="bckgrnds/icons/port_ico.gif" align=sidebar></a>A <b>union</b> is declared in the same format as a <b>struct</b> or a 
  1583. <b>class</b>. The <b>union</b> declaration<br>
  1584. <font size=2><br></font><font size=11><pre>
  1585. union Number {<p>
  1586.    int x;<p>
  1587.    float y;<p>
  1588. };<p>
  1589. </pre></font>
  1590. indicates that <b>Number</b> is a <b>union</b> type with members 
  1591. <b>int</b> <b>x</b> and <b>float</b> <b>y</b>. <a href="^Engineer::c:s0p5"><img src="bckgrnds/icons/seo_ico.gif" align=sidebar></a>The <b>union</b> definition normally 
  1592. precedes <b>main</b> in a program so the definition can be 
  1593. used to declare variables in all the program's functions. <br>
  1594.  
  1595. </page>
  1596. <page>
  1597. The only valid built-in operations that can be performed 
  1598. on a <b>union</b> are: assigning a <b>union</b> to another <b>union</b> of 
  1599. the same type, taking the address (<b>&</b>) of a <b>union</b>, and 
  1600. accessing <b>union</b> members using the structure member 
  1601. operator (<b>.</b>) and the structure pointer operator (<b>-></b>). 
  1602. <b>union</b>s may not be <a href="^Errors::c:s0p3"><img src="bckgrnds/icons/cpe_ico.gif" align=sidebar></a>compared for the same reasons that 
  1603. structures cannot be compared.<br>
  1604. <spacer width=16 height=1>A <b>union</b> is similar to a class in that it can have a 
  1605. constructor to initialize any of its members. A <b>union</b> 
  1606. that has no constructor can be initialized with another 
  1607. <b>union</b> of the same <a href="^Errors::c:s0p2"><img src="bckgrnds/icons/cpe_ico.gif" align=sidebar></a>type, with an expression of the type 
  1608. of the first member of the <b>union</b>, or with an initializer 
  1609. (enclosed in braces) of the type of the first member of <br>
  1610.  
  1611. </page>
  1612. <page>
  1613. the <b>union</b>. <b>union</b>s can have other member functions, 
  1614. such as destructors, but a <tt><b>union</b></tt>'s member functions 
  1615. cannot be declared <b>virtual</b>. The members of a <b>union</b> 
  1616. are <b>public</b> by default. <br>
  1617. <spacer width=16 height=1>A <b>union</b> cannot be used as a base class in inheritance, 
  1618. i.e., classes may not be derived from <b>union</b>s. <b>union</b>s 
  1619. can have objects as members only if these objects do 
  1620. not have a constructor, a destructor, or an overloaded 
  1621. assignment operator. None of a <b>union</b>'s data members 
  1622. can be declared <b>static</b>.<br>
  1623. <spacer width=16 height=1>The program in<a href="^Code::c:s0p5"> <img src="bckgrnds/icons/code_ico.gif" align=sidebar>Fig. 18.8</a> uses the variable <b>value</b> of 
  1624. type <b>union</b> <b>number</b> to display the value stored in the 
  1625. <b>union</b> as both an <b>int</b> and a <b>float</b>. The program output is <br>
  1626.  
  1627. </page>
  1628. <page>
  1629. implementation dependent. The program output shows 
  1630. that the internal representation of a <b>float</b> value can be 
  1631. quite different from the representation of an <b>int</b>.<br>
  1632. <spacer width=16 height=1>An <i>anonymous <b>union</b></i> is a <b>union</b> without a type name, 
  1633. that does not attempt to define objects or pointers 
  1634. before its terminating semicolon. Such a <b>union</b> does not 
  1635. create a type but does create an unnamed object. An 
  1636. anonymous <b>union</b>'s members may be accessed directly 
  1637. in the scope in which the anonymous <b>union</b> is declared 
  1638. just as any other local variable--there is no need to use 
  1639. the dot (<b>.</b>) or arrow (<b>-></b>) operators. <br>
  1640. <spacer width=16 height=1>Anonymous <b>union</b>s have some restrictions. 
  1641. Anonymous <b>union</b>s can contain only data members. All <br>
  1642.  
  1643. </page>
  1644. <page>
  1645. members of an anonymous <b>union</b> must be <b>public</b>. And, 
  1646. an anonymous <b>union</b> declared globally (i.e., at file 
  1647. scope) must be explicitly declared <b>static</b>. <br>
  1648. <spacer width=16 height=1><a href="^Code::c:s0p6"><img src="bckgrnds/icons/code_ico.gif" align=sidebar>Figure 18.9</a> illustrates the use of an anonymous <b>union</b>.<br>
  1649.  
  1650. </page>
  1651. <page>
  1652. <b>Select the true statement(s). </b><br>
  1653. <component type="checkbox" width=20 height=18 label="" name=""  feedback="False. The members are public by default.">
  1654. The members of a union are private by default.   <br>
  1655. <component type="checkbox" width=20 height=18 label="" name=""  correct="True.">
  1656. All union members share the same address space.   <br>
  1657. <component type="checkbox" width=20 height=18 label="" name=""  feedback="False. union definitions can be placed anywhere a struct or class can be placed.">
  1658. Unlike structs or classes, union definitions must be placed in a header file.   <br>
  1659. <component type="checkbox" width=20 height=18 label="" name=""  correct="True.">
  1660. An anonymous union is a union without a type name, that does not attempt to define objects or pointers before its terminating semicolon.   <br>
  1661. <component type=button name=b label="Check Your Answer" width=125 height=24>
  1662.  
  1663. </page>
  1664. </section>
  1665. <section type=Body name=Default title="18.13 Linkage Specifications ">
  1666. <page>
  1667. <font size=18 bold>18.13 Linkage Specifications </font><hr>
  1668. It is possible from a C++ program to call functions 
  1669. written and compiled with a C compiler. As stated in 
  1670. Section 3.20, C++ specially encodes function names for 
  1671. type-safe linkage. C, however, does not encode its 
  1672. function names. Thus, a function compiled in C will not 
  1673. be recognized when an attempt is made to link C code 
  1674. with C++ code because the C++ code expects a 
  1675. specially encoded function name. C++ enables the 
  1676. programmer to provide <i>linkage specifications</i> to inform 
  1677. the compiler that a function was compiled on a C 
  1678. compiler, and to prevent the name of the function from <br>
  1679.  
  1680. </page>
  1681. <page>
  1682. being encoded by the C++ compiler. Linkage 
  1683. specifications are useful when large libraries of 
  1684. specialized functions have been developed, and the user 
  1685. either does not have access to the source code for 
  1686. recompilation into C++ or time to convert the library 
  1687. functions from C to C++. <br>
  1688. <spacer width=16 height=1>To inform the compiler that one or several functions 
  1689. have been compiled in C, write the function prototypes 
  1690. as follows:<br>
  1691. <font size=2><br></font><font size=11><pre>
  1692. extern "C" <i>function prototype</i>   // single function<p>
  1693. <p>
  1694. extern "C"   // multiple functions<p>
  1695. {<p><p>
  1696. </pre></font>
  1697.  
  1698. </page>
  1699. <page>
  1700. <font size=2><br></font><font size=11><pre>
  1701.    <i>function prototypes<p>
  1702. </i>}<p>
  1703. </pre></font>
  1704. These declarations inform the compiler that the 
  1705. specified functions are not compiled in C++, so name 
  1706. encoding should not be performed on the functions 
  1707. listed in the linkage specification. These functions can 
  1708. then be linked properly with the program. C++ 
  1709. environments normally include the standard C libraries 
  1710. and do not require the programmer to use linkage 
  1711. specifications for those functions.<br>
  1712.  
  1713. </page>
  1714. </section>
  1715. <section type=Body name=Default title="18.14 Summary">
  1716. <page>
  1717. <font size=18 bold>18.14 Summary</font><hr>
  1718. <indent width=8 delay>*   On many systems--UNIX and DOS systems in particular--it is possible to redirect input to a program and 
  1719. output from a program. Input is redirected from the 
  1720. UNIX and DOS command lines using the redirect input 
  1721. symbol (<b><</b>) or using a pipe (<b>|</b>). Output is redirected from 
  1722. the UNIX and DOS command lines using the redirect 
  1723. output symbol (<b>></b>) or the append output symbol (<b>>></b>). 
  1724. The redirect output symbol simply stores the program 
  1725. output in a file and the append output symbol appends 
  1726. the output to the end of a file.</indent>
  1727. <indent width=8 delay>*  The macros and definitions of the variable arguments </indent>
  1728.  
  1729. </page>
  1730. <page>
  1731. <indent width=8 delay>*   header <b>stdarg.h</b> provide the capabilities necessary to 
  1732. build functions with variable-length argument lists. </indent>
  1733. <indent width=8 delay>*   An ellipsis (<b>...</b>) in a function prototype indicates that 
  1734. the function receives a variable number of arguments.</indent>
  1735. <indent width=8 delay>*   Type <b>va_list</b> is suitable for holding information 
  1736. needed by macros <b>va_start</b>, <b>va_arg</b>, and <b>va_end</b>. To 
  1737. access the arguments in a variable-length argument list, 
  1738. an object of type <b>va_list</b> must be declared.</indent>
  1739. <indent width=8 delay>*   Macro <b>va_start</b> is invoked before the arguments of a 
  1740. variable-length argument list can be accessed. The 
  1741. macro initializes the object declared with <b>va_list</b> for use 
  1742. by the <b>va_arg</b> and <b>va_end</b> macros.</indent>
  1743. <indent width=8 delay>*  Macro <b>va_arg</b> expands to an expression of the value </indent>
  1744.  
  1745. </page>
  1746. <page>
  1747. <indent width=8 delay>*   and type of the next argument in the variable-length 
  1748. argument list. Each invocation of <b>va_arg</b> modifies the 
  1749. object declared with <b>va_list</b> so that the object points to 
  1750. the next argument in the list.</indent>
  1751. <indent width=8 delay>*   Macro <b>va_end</b> facilitates a normal return from a 
  1752. function whose variable argument list was referred to 
  1753. by the <b>va_start</b> macro.</indent>
  1754. <indent width=8 delay>*   On many systems--DOS and UNIX in particular--it 
  1755. is possible to pass command-line arguments to <b>main</b> by 
  1756. including in <b>main</b>'s parameter list the parameters <b>int</b> 
  1757. <b>argc</b> and <b>char *argv[]</b>. Parameter <b>argc</b> is the number 
  1758. of command-line arguments. Parameter <b>argv</b> is an array 
  1759. of strings containing the command-line arguments. </indent>
  1760.  
  1761. </page>
  1762. <page>
  1763. <indent width=8 delay>*   The definition of a function must be entirely contained in one file--it cannot span two or more files.</indent>
  1764. <indent width=8 delay>*   Global variables must be declared in each file in 
  1765. which they are used. </indent>
  1766. <indent width=8 delay>*   Function prototypes can extend the scope of a function beyond the file in which it is defined (the <b>extern</b> 
  1767. specifier is not required in a function prototype). This is 
  1768. accomplished by including the function prototype in 
  1769. each file in which the function is invoked and compiling the files together.</indent>
  1770. <indent width=8 delay>*  The storage class specifier <b>static</b>, when applied to a 
  1771. global variable or a function, prevents it from being 
  1772. used by any function that is not defined in the same file. </indent>
  1773.  
  1774. </page>
  1775. <page>
  1776. <indent width=8 delay>*   This is referred to as internal linkage. Global variables 
  1777. and functions that are not preceded by <b>static</b> in their 
  1778. definitions have external linkage--they can be accessed 
  1779. in other files if those files contain proper declarations 
  1780. and/or function prototypes. </indent>
  1781. <indent width=8 delay>*   The <b>static</b> specifier is commonly used with utility 
  1782. functions that are called only by functions in a particular file. If a function is not required outside a particular 
  1783. file, the principle of least privilege should be enforced 
  1784. by using <b>static</b>. </indent>
  1785. <indent width=8 delay>*  When building large programs in multiple source 
  1786. files, compiling the program becomes tedious if small 
  1787. changes are made to one file, and the entire program </indent>
  1788.  
  1789. </page>
  1790. <page>
  1791. <indent width=8 delay>*   must be recompiled. Many systems provide special utilities that recompile only the modified program file. On 
  1792. UNIX systems the utility is called <b>make</b>. Utility <b>make</b> 
  1793. reads a file called <b>makefile</b> that contains instructions 
  1794. for compiling and linking the program. </indent>
  1795. <indent width=8 delay>*   Function <b>exit</b> forces a program to terminate as if it 
  1796. executed normally.</indent>
  1797. <indent width=8 delay>*   Function <b>atexit</b> registers a function in a program to 
  1798. be called upon normal termination of the program--i.e., 
  1799. either when the program terminates by reaching the end 
  1800. of <b>main</b> or when <b>exit</b> is invoked.</indent>
  1801. <indent width=8 delay>*  Function <b>atexit</b> takes a pointer to a function (i.e., a 
  1802. function name) as an argument. Functions called at pro</indent>
  1803.  
  1804. </page>
  1805. <page>
  1806. <indent width=8 delay>*   gram termination cannot have arguments, and cannot 
  1807. return a value. Up to 32 functions may be registered for 
  1808. execution at program termination. </indent>
  1809. <indent width=8 delay>*   Function <b>exit</b> takes one argument. The argument is 
  1810. normally the symbolic constant <b>EXIT_SUCCESS</b> or 
  1811. the symbolic constant <b>EXIT_FAILURE</b>. If <b>exit</b> is 
  1812. called with <b>EXIT_SUCCESS</b>, the implementation-
  1813. defined value for successful termination is returned to 
  1814. the calling environment. If <b>exit</b> is called with 
  1815. <b>EXIT_FAILURE</b>, the implementation-defined value 
  1816. for unsuccessful termination is returned. </indent>
  1817. <indent width=8 delay>*  When function <b>exit</b> is invoked, any functions registered with <b>atexit</b> are invoked in the reverse order of </indent>
  1818.  
  1819. </page>
  1820. <page>
  1821. <indent width=8 delay>*   their registration, all streams associated with the program are flushed and closed, and control returns to the 
  1822. host environment. </indent>
  1823. <indent width=8 delay>*   The <b>volatile</b> qualifier is used to prevent optimizations of a variable because it can be modified from outside the program's scope.</indent>
  1824. <indent width=8 delay>*  C++ provides integer and floating-point suffixes for 
  1825. specifying the types of integer and floating-point constants. The integer suffixes are: <b>u</b> or <b>U</b> for an <b>unsigned</b> 
  1826. integer, <b>l</b> or <b>L</b> for a <b>long</b> integer, and <b>ul</b> or <b>UL</b> for an 
  1827. <b>unsigned long</b> integer. If an integer constant is not suffixed, its type is determined by the first type capable of 
  1828. storing a value of that size (first <b>int</b>, then <b>long int</b>, then </indent>
  1829.  
  1830. </page>
  1831. <page>
  1832. <indent width=8 delay>*   <b>unsigned long int</b>). The floating-point suffixes are: <b>f</b> or 
  1833. <b>F</b> for a <b>float</b>, and l<b> </b>or <b>L</b> for a <b>long double</b>. A floating-
  1834. point constant that is not suffixed is of type <b>double</b>.</indent>
  1835. <indent width=8 delay>*   The signal handling library provides the capability to 
  1836. trap unexpected events with function <b>signal</b>. Function 
  1837. <b>signal</b> receives two arguments--an integer signal number and a pointer to the signal handling function. </indent>
  1838. <indent width=8 delay>*   Signals can also be generated with function <b>raise</b> and 
  1839. an integer argument.</indent>
  1840. <indent width=8 delay>*   The general utilities library (<b>stdlib.h</b>) provides functions <b>calloc</b> and <b>realloc</b> for dynamic memory allocation. These functions can be used to create dynamic 
  1841. arrays. </indent>
  1842.  
  1843. </page>
  1844. <page>
  1845. <indent width=8 delay>*   Function <b>calloc</b> receives two arguments--the number of elements (<b>nmemb</b>) and the size of each element 
  1846. (<b>size</b>)--and initializes the elements of the array to zero. 
  1847. The function returns either a pointer to the allocated 
  1848. memory, or a <b>NULL</b> pointer if the memory is not allocated. </indent>
  1849. <indent width=8 delay>*   Function <b>realloc</b> changes the size of an object allocated by a previous call to <b>malloc</b>, <b>calloc</b>, or <b>realloc</b>. 
  1850. The original object's contents are not modified provided that the amount of memory allocated is larger 
  1851. than the amount allocated previously. </indent>
  1852. <indent width=8 delay>*  Function <b>realloc</b> takes two arguments--a pointer to 
  1853. the original object (<b>ptr</b>) and the new size of the object </indent>
  1854.  
  1855. </page>
  1856. <page>
  1857. <indent width=8 delay>*   (<b>size</b>). If <b>ptr</b> is <b>NULL</b>, <b>realloc</b> works identically to 
  1858. <b>malloc</b>. If <b>size</b> is <b>0</b> and the pointer received is not 
  1859. <b>NULL</b>, the memory for the object is freed. Otherwise, 
  1860. if <b>ptr</b> is not <b>NULL</b> and <b>size</b> is greater than zero, <b>realloc</b> 
  1861. tries to allocate a new block of memory for the object. 
  1862. If the new space cannot be allocated, the object pointed 
  1863. to by <b>ptr</b> is unchanged. Function <b>realloc</b> returns either 
  1864. a pointer to the reallocated memory, or a <b>NULL</b> pointer.</indent>
  1865. <indent width=8 delay>*   The result of the <b>goto</b> statement is a change in the 
  1866. program's flow of control. Program execution continues at the first statement after the label in the <b>goto</b> statement. </indent>
  1867. <indent width=8 delay>*  A label is an identifier followed by a colon. A label </indent>
  1868.  
  1869. </page>
  1870. <page>
  1871. <indent width=8 delay>*   must appear in the same function as the <b>goto</b> statement 
  1872. that refers to it. </indent>
  1873. <indent width=8 delay>*   A <b>union</b> is a derived data type whose members share 
  1874. the same storage space. The members can be any type. 
  1875. The storage reserved for a <b>union</b> is large enough to 
  1876. store its largest member. In most cases, <b>union</b>s contain 
  1877. two or more data types. Only one member, and thus one 
  1878. data type, can be referenced at a time.</indent>
  1879. <indent width=8 delay>*   A <b>union</b> is declared in the same format as a structure. </indent>
  1880. <indent width=8 delay>*   A <b>union</b> can be initialized only with a value of the 
  1881. type of its first member. </indent>
  1882. <indent width=8 delay>*  C++ enables the programmer to provide linkage 
  1883. specifications to inform the compiler that a function </indent>
  1884.  
  1885. </page>
  1886. <page>
  1887. <indent width=8 delay>*   was compiled on a C compiler, and to prevent the name 
  1888. of the function from being encoded by the C++ compiler. </indent>
  1889. <indent width=8 delay>*   To inform the compiler that one or several functions 
  1890. have been compiled in C, write the function prototypes 
  1891. as follows:</indent>
  1892. <font size=2><br></font><font size=11><pre>
  1893. extern "C" <i>function prototype</i>   // single function<p>
  1894. extern "C"   // multiple functions<p>
  1895. {<p>
  1896.    <i>function prototypes</i><p>
  1897. }<p>
  1898. </pre></font>
  1899. <indent width=8 delay>*  These declarations inform the compiler that the specified functions are not compiled in C++, so name </indent>
  1900.  
  1901. </page>
  1902. <page>
  1903. <indent width=8 delay>*   encoding should not be performed on the functions 
  1904. listed in the linkage specification. These functions can 
  1905. then be linked properly with the program.</indent>
  1906. <indent width=8 delay>*   C++ environments normally include the standard C 
  1907. libraries and do not require the programmer to use linkage specifications for those functions.</indent>
  1908.  
  1909. </page>
  1910. <page>
  1911. <br>
  1912.  
  1913. </page>
  1914. <page>
  1915.  
  1916. </page>
  1917. </section>
  1918.  
  1919. <section type=Popup name=Debug title="Testing">
  1920. <page>
  1921. This chapter does not contain any Testing and Debugging tips.
  1922. </page>
  1923. </section>
  1924. <section type=Popup name=AppletPopup title="Applet Examples">
  1925. <page>
  1926. This chapter does not contain any Applet Examples.
  1927. </page>
  1928. </section>
  1929. <section type=Popup name=Practice title="Good Practices">
  1930. <page>
  1931. This chapter does not contain any Good Programming Practices tips.
  1932. </page>
  1933. </section>
  1934. </chapter>
  1935. </html>
  1936. </html>
  1937.